package me.xiaosiqi.server_info.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import me.xiaosiqi.server_info.comm.BaseResponse;
import me.xiaosiqi.server_info.comm.User;
import me.xiaosiqi.server_info.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


/**
 * @author xiaosiqi
 * @date 2021/9/13 21:11
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/login")
    public BaseResponse login(@RequestParam String username,@RequestParam String password, HttpServletRequest request) {
        User loginUser = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
        if (null != loginUser) {
            if (StrUtil.equals(password, loginUser.getPassword())) {
                request.getSession().setAttribute("login", loginUser);
                return BaseResponse.ok("登录成功！");
            }
        }
        return BaseResponse.error("用户名或密码错误");
    }

}
